<?php

/**
 * Description of Helpers_ProductTable
 *
 * @author a.novikov
 */
class Helpers_ProductTableNerf2 extends Helpers_Base_Paginator {

	const IS_PRESENT = 'is_present';
	const ORDER = 'order';
	const ORDER_DIRECTION = 'order_direction';
	const VIEW_PATH = 'layouts/blocks/product_nerf2_table.tpl';


	/**
	 *
	 * @return array
	 */
	protected function getPageParams() {

		$isPresent = (bool)$this->_getParam('is_present', false);
		if (isset($_COOKIE[self::IS_PRESENT])) {
			$isPresent = (boolean)$_COOKIE[self::IS_PRESENT];
		}

		$orderTypes = array(
			'rank' => 'Популярности',
			'price'=>'Цене',
			'name'=>'Алфавиту',
			'rating'=>'Рейтингу'
		);
		
		$order = $this->_getParam('order', 'rank');

		if (isset($_COOKIE[self::ORDER])) {
			$order = $_COOKIE[self::ORDER];
		}

		if ( !isset($orderTypes[$order]) ) {
			$order = 'name';
		}
		
		$orderDirection = $this->_getParam('order_direction', 'desc');
		
		if (isset($_COOKIE[self::ORDER_DIRECTION])) {
			$orderDirection = $_COOKIE[self::ORDER_DIRECTION];
		}


		if ($orderDirection != 'asc') {
			$orderDirection = 'desc';
		}

		return array(
			'paginatorParams' => $this->getPaginatorParams(),
			'isPresent'	      => $isPresent,
			'order'			  => array(
								'field'     => $order,
								'direction'	=> $orderDirection
			),
			'orderTypes' => $orderTypes
		);
	}

	public function ProductTableNerf2(Db_Select $productSelect, $product_select_temp = null) {
		extract( $this->getPageParams() );

		//$productSelect->where(ProductPeer::FILE_IMAGE.' is not null');
		$productSelect->setIntegrityCheck(false);
		$productSelect->join('product_collection', 'product_collection.id = product.collection_id', array('col_key' => 'key'));
		$productSelect->where(ProductPeer::IS_ACTIVE, 1);
		$productSelect->where('product_collection.key = "dart_tag"');

		$productSelect->order($order['field'].' '.$order['direction']);
		$productSelect->order('id desc');
		//die($productSelect);
		//exit;
		//die($productSelect);
		//@todo рассмотреть возможность мягкой замены на флаг is_present
		/* есть даже загвоздка, кажется ни кто не учитывал что может возникнуть 
		 * ситуация когда подтовар есть а товара-карточки нет, в этом случае данный флаг бесполезен
		 */
		if ($isPresent) {
			$productSelect->where(ProductPeer::IS_PRESENT.'= ?', 1);
			//$productSelect->where(ProductPeer::COUNT.' > 0');
		}

		$productPeer = ProductPeer::getInstance();
		$paginator = ProductService::getInstance()->getPaginatorBySelect(
			$productPeer, $productSelect, $paginatorParams
		);
		$per_page = $paginatorParams['per_page'];
		if (isset($_COOKIE['per_page'])) {
			$per_page = (int)$_COOKIE['per_page'];
		}

		$this->view->per_page = $per_page;
		$this->view->order = $order['field'];
		$this->view->order_direction =  $order['direction'];
		
		$this->view->is_present = $isPresent;
		$this->view->order_types = $orderTypes;

		$this->view->products_paginator2 = $paginator;
		
		$this->view->products = ContentList::factoryByClassName('List_Product_CategoryTemp', $paginator);

		return $this->render();
	}

	public function getViewPath(){
		return self::VIEW_PATH;
	}
}